package lion.framework.core.web.request;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.StringUtils;

import lion.dev.lang.MapJ;
import lion.dev.web.WebUtil;
import lion.dev.web.useragent.UserAgent;
import lion.dev.web.useragent.UserAgentParser;
import lion.framework.core.web.request.multi.MultiRequestWrapper;

/**
 * 对httpServeltRequest 的封装，更便捷的获取请求参数、请求属性
 *
 * <pre>
 * time   2013 2013-4-24 上午11:32:18
 * mail   hl_0074@sina.com
 * </pre>
 *
 * @author lion
 */
public class Request extends MapJ {

	private static final long serialVersionUID = -7829053669469271303L;
	private String sessionId;
	private MapJ session; // 会话
	private MapJ models; // 设置的值
	private HttpServletRequest httpRequest;
	private UserAgent userAgent;
	private String bodyContent;

	public Request(HttpServletRequest request) {

		this.httpRequest = request;
		this.sessionId = request.getSession().getId();
		this.putAll(MapJ.mapRequest(this.httpRequest.getParameterMap()));
	}

	public String getBodyString() {
		if (bodyContent == null) {
			bodyContent = WebUtil.getRequestBody(httpRequest);
		}
		return bodyContent;
	}

	public List<FileItem> getFileItems() {

		if (ServletFileUpload
				.isMultipartContent(httpRequest)) { return ((MultiRequestWrapper) httpRequest).getUploadFileItems(); }
		return Collections.emptyList();
	}

	public FileItem[] getFileItems(String name) {

		if (ServletFileUpload.isMultipartContent(
				httpRequest)) { return ((MultiRequestWrapper) httpRequest).getUploadFileItems(name); }
		return new FileItem[0];
	}

	public FileItem getFileItem(String name) {

		if (ServletFileUpload.isMultipartContent(
				httpRequest)) { return ((MultiRequestWrapper) httpRequest).getUploadFileItem(name); }
		return null;
	}

	public MapJ getSession() {

		if (this.session == null) {
			this.session = new RequestSession(this.httpRequest.getSession());
		}
		return session;
	}

	public void addSession(String key, Object value) {

		this.getSession().put(key, value);
	}

	public String getSessionId() {

		return sessionId;
	}

	public void addModel(String key, Object value) {

		this.httpRequest.setAttribute(key, value);
	}

	public UserAgent getUserAgent() {

		if (this.userAgent == null) {
			this.userAgent = UserAgentParser.parse(this.httpRequest.getHeader("User-Agent"));
		}
		return this.userAgent;
	}

	public void addError(String key, String msg) {

		MapJ errors = (MapJ) this.httpRequest.getAttribute("error");
		if (errors == null) {
			errors = new MapJ();
			this.addModel("error", errors);
		}
		errors.put(key, msg);
	}

	public String getRemoteAddr() {

		return WebUtil.getRemoteIP(httpRequest);
	}

	public MapJ getModels() {

		if (this.models == null) {
			this.models = new RequestModel(this.httpRequest);
		}
		return this.models;
	}

	public boolean isAjax() {

		return StringUtils.equalsIgnoreCase("XMLHttpRequest", this.httpRequest.getHeader("X-Requested-With"));
	}

}
